package com.situ.mall.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.common.entity.Category;
import com.situ.mall.common.service.ICategoryService;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    //打开分类管理页面
    @GetMapping("/page")
    public String page() {
        return "category";
    }

    //返回分类树状列表数据
    @GetMapping("/tree")
    @ResponseBody
    public TableVO tree() {
        return categoryService.tree();
    }

    //保存分类
    @PostMapping("/save")
    @ResponseBody
    public void save(Category category) {
        categoryService.saveOrUpdate(category);
    }

    //根据ID查分类
    @GetMapping("/{id}")
    @ResponseBody
    public Category info(@PathVariable Integer id) {
        return categoryService.getById(id);
    }

    //根据ID删除分类
    @PostMapping("/delete/{id}")
    @ResponseBody
    public void delete(@PathVariable Integer id) {
        categoryService.doDelete(id);
    }

    @GetMapping("/children")
    @ResponseBody
    public List<Category> list2(Integer id){
        //查询条件构造器
        QueryWrapper<Category> wq = new QueryWrapper<>();
        wq.eq("parent_id", id);
        return categoryService.list(wq);
    }
}
